package br.net.woodstock.tickettoride.web.jsf.access.common;

import java.util.Collection;

import br.net.woodstock.tickettoride.api.access.UserService;
import br.net.woodstock.tickettoride.orm.User;
import br.net.woodstock.tickettoride.orm.UserType;
import br.net.woodstock.tickettoride.web.jsf.TTRBean;

public abstract class AbstractUserBean extends TTRBean {

	private static final long	serialVersionUID	= 365982448995745841L;

	private static final String	MAIN_VIEW			= "/access/user.xhtml";

	private static final String	LIST_VIEW			= "/access/user-list.xhtml";

	private static final String	SAVE_VIEW			= "/access/user-save.xhtml";

	private Integer				id;

	private String				login;

	private String				name;

	private String				email;

	private String				password;

	private String				type;

	private String				view;

	private String				filter;

	private Collection<User>	users;

	public AbstractUserBean() {
		super();
		this.view = AbstractUserBean.LIST_VIEW;
	}

	public abstract UserService getUserService();

	public String start() {
		this.onStart();
		return AbstractUserBean.MAIN_VIEW;
	}

	public String end() {
		this.onEnd();
		return TTRBean.INDEX_VIEW;
	}

	public String newUser() {
		this.id = null;
		this.login = null;
		this.name = null;
		this.email = null;
		this.password = null;
		this.type = null;
		this.view = AbstractUserBean.SAVE_VIEW;
		return null;
	}

	public String list() {
		this.users = this.getUserService().listByFilter(this.filter);
		return null;
	}

	public String edit() {
		User user = this.getUserService().getById(this.id);
		this.id = user.getId();
		this.login = user.getLogin();
		this.name = user.getName();
		this.email = user.getEmail();
		this.password = user.getPassword();
		this.type = user.getType().name();
		this.view = AbstractUserBean.SAVE_VIEW;
		return null;
	}

	public String save() {
		User user = new User();
		user.setLogin(this.login);
		user.setName(this.name);
		user.setEmail(this.email);
		user.setPassword(this.password);
		user.setType(UserType.valueOf(this.type));

		if (this.isValidId(this.id)) {
			user.setId(this.id);
			this.getUserService().update(user);
		} else {
			this.getUserService().save(user);
			this.id = null;
			this.login = null;
			this.name = null;
			this.email = null;
			this.password = null;
			this.type = null;
		}

		this.addMessage(this.getMessageOperationOK());

		return null;
	}

	public String cancel() {
		this.view = AbstractUserBean.LIST_VIEW;
		if (this.users != null) {
			this.list();
		}
		return null;
	}

	// Getters and Setters
	public Integer getId() {
		return this.id;
	}

	public void setId(final Integer id) {
		this.id = id;
	}

	public String getLogin() {
		return this.login;
	}

	public void setLogin(final String login) {
		this.login = login;
	}

	public String getName() {
		return this.name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(final String email) {
		this.email = email;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public String getType() {
		return this.type;
	}

	public void setType(final String type) {
		this.type = type;
	}

	public String getView() {
		return this.view;
	}

	public void setView(final String view) {
		this.view = view;
	}

	public String getFilter() {
		return this.filter;
	}

	public void setFilter(final String filter) {
		this.filter = filter;
	}

	public Collection<User> getUsers() {
		return this.users;
	}

	public void setUsers(final Collection<User> users) {
		this.users = users;
	}

}
